<div class="widget-search widget">
	<form class="widget-search__form" method="GET">
		<label>
			<input id="searchContent" class="widget-search__field" type="input" placeholder="{{ T "search_placeholder" }}" value="" onkeyup="getSearch()">
		</label>
	</form>
	<div style="width: 100%; height: auto; background-color:#f5f5f5;" id="searchResult"></div>

</div>


<script type="text/javascript">

	var items = null

	window.onload = function() {
	  	document.onkeydown = function(ev) {
			var items = getItems()
			var event = ev || event
			if (event.keyCode == 13) {
				return false;
			}
		  }
		items = getItems()
	}

	function getSearch(){
		var ritems = search(items)
		fomatResult(ritems)
	}


	function getItems(){
		xmltext = new XMLHttpRequest;
    	xmltext.open("GET", "/index.xml", false);
    	xmltext.send();
    	resp = xmltext.responseXML;
		items = resp.getElementsByTagName("item");
		return items;
	}

	function search(items){
		var content = document.getElementById("searchContent").value.toLowerCase();
		if(content == ''){
			document.getElementById("searchResult").innerHTML= ''
			return 
		}
		var ritems = new Array();
		for(i=0; i<items.length; i++){
			if((items[i].getElementsByTagName("title")[0].innerHTML).toLowerCase().indexOf(content)>-1){
				ritems.push(items[i]);
			}
		}
		return ritems
	}

	function fomatResult(items){
		if(items.length <= 0){
			document.getElementById("searchResult").innerHTML=""
		}else{
			var innerHTMLs = "<table>"
			innerHTMLs += "<tr><td>站内搜索：</td></tr>"
			for(i=0; i<items.length; i++){
				innerHTMLs+=( "<tr><td>"+ (i+1) +". <a href='"+ items[i].getElementsByTagName("link")[0].innerHTML +"'>" + items[i].getElementsByTagName("title")[0].innerHTML + "</a></td></tr>" )
			}
			innerHTMLs += "</table>"
			document.getElementById("searchResult").innerHTML= innerHTMLs
		}
	}

</script>